﻿// -----------------------------------------------------------------------
// <copyright file="ExportWebCmdLet.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SPMigrationCmdlets
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Management.Automation;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Xml;
    //using SharePoint.ImportExport.Core;
    using System.Data;
    using Sharepoint.ImportExport.Extractor;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Deployment;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [Cmdlet(VerbsData.Export, "SPMigrationDataForSPO")]
    public class ExportWebCmdLet : PSCmdlet
    {
        private string url;
        private string folder;
        private string file;

        private string[] lists;

        /// <summary>
        /// Gets or sets the URL to the SharePoint site to be exported
        /// </summary>
        [Parameter(Mandatory = true)]
        public string URL
        {
            get
            {
                return this.url;
            }

            set
            {
                this.url = value;
            }
        }

        /// <summary>
        /// Gets or sets the Folder
        /// </summary>
        [Parameter(Mandatory = true)]
        public string Folder
        {
            get
            {
                return this.folder;
            }

            set
            {
                this.folder = value;
            }
        }


        /// <summary>
        /// Gets or sets the Folder
        /// </summary>
        [Parameter(Mandatory = false)]
        public string[] Lists
        {
            get
            {
                return this.lists;
            }

            set
            {
                this.lists = value;
            }
        }
        

        /// <summary>
        /// Gets or sets the File
        /// </summary>
        [Parameter(Mandatory = false)]
        public string File
        {
            get
            {
                return this.file;
            }

            set
            {
                this.file = value;
            }
        }
        protected override void ProcessRecord()
        {
            Guid g = Guid.Empty;
            string s = this.URL;
            SPExportSettings settings = new SPExportSettings();
                
            using (SPSite site = new SPSite(s))
            {
                SPWeb web = site.RootWeb;

                if (site.Url != s)
                {
                    s = s.Substring(site.Url.Length + 1);
                    g = site.AllWebs[s].ID;
                    web = site.AllWebs[s];
                }
                else
                {
                    g = site.RootWeb.ID;
                }

                settings.FileLocation = this.Folder;
                //settings.BaseFileName = this.File;
                settings.SiteUrl = this.URL;

                settings.ExportMethod = SPExportMethodType.ExportAll;
                if (String.IsNullOrEmpty(this.File))
                {
                    settings.FileCompression = false;
                }
                else
                {
                    settings.FileCompression = false;
                }

                settings.IncludeVersions = SPIncludeVersions.All;
                settings.IncludeSecurity = SPIncludeSecurity.All;
                settings.ExcludeDependencies = false;
                settings.ExportFrontEndFileStreams = true;
                settings.OverwriteExistingDataFile = true;
                settings.FileCompression = false;
                
                if (Lists.Length == 0)
                {
                    SPExportObject expObj = new SPExportObject();
                    expObj.IncludeDescendants = SPIncludeDescendants.All;
                    expObj.ExcludeChildren = true;
                    expObj.Id = g;
                    expObj.Type = SPDeploymentObjectType.Web;
                    settings.ExportObjects.Add(expObj);
                }
                else
                {
                    foreach (SPList list in web.Lists)
                    { 
                        foreach(string listName in lists)
                        {
                            if(list.Title==listName)
                            {
                                SPExportObject expObj = new SPExportObject();
                                expObj.IncludeDescendants = SPIncludeDescendants.All;
                                expObj.ExcludeChildren = true;
                                expObj.Id = list.ID;
                                expObj.Type = SPDeploymentObjectType.List;
                                settings.ExportObjects.Add(expObj);
                            }
                        }
                        
                    }
                }

            }
            
            SPExport export = new SPExport(settings);
            export.Run();

        }

    }
}
